﻿using System;
using System.Drawing;
using Dataweb.NShape.Advanced;
using Dataweb.NShape;
using cz.zcu.PreSti.Components;

namespace cz.zcu.PreSti.Shapes
{
    public class EntryPointSymbol : RectangleBase, ISymbol
    {
        protected override void InitializeToDefault(IStyleSet styleSet)
        {
            base.InitializeToDefault(styleSet);
            Height = 20;
            Width = 20;
        }

        public override Shape Clone()
        {
            Shape result = new EntryPointSymbol(Type, (Template)null);
            result.CopyFrom(this);
            return result;
        }

        protected override int ControlPointCount
        {
            get { return ActionsCount + EventsCount + SettersCount + GettersCount; }
        }

        public override bool HasControlPointCapability(ControlPointId controlPointId, ControlPointCapabilities controlPointCapability)
        {
            switch (controlPointId)
            {
                case 1:
                    return ((controlPointCapability & ControlPointCapabilities.Connect) != 0);
                default:
                    return base.HasControlPointCapability(controlPointId, controlPointCapability);
            }
        }


        protected internal EntryPointSymbol(ShapeType shapeType, Template template)
            : base(shapeType, template)
        {
        }


        protected override void CalcControlPoints()
        {

            int left = (int)Math.Round(-Width / 2f);
            int top = (int)Math.Round(-Height / 2f);
            int right = left + Width;
            int bottom = top + Height;

            ControlPoints[0].X = right;
            ControlPoints[0].Y = top + Height / 2;
        }


        protected override void CalcCaptionBounds(int index, out Rectangle captionBounds)
        {
            if (index != 0) throw new IndexOutOfRangeException();
            int left = (int)Math.Round(-Width / 2f);
            int top = (int)Math.Round(-Height / 2f);
            captionBounds = Rectangle.Empty;
            captionBounds.X = left;
            captionBounds.Y = top;
            captionBounds.Width = Width;
            captionBounds.Height = Height;
        }


        protected override bool CalculatePath()
        {
            if (base.CalculatePath())
            {
                int left = (int)Math.Round(-Width / 2f);
                int top = (int)Math.Round(-Height / 2f);
                int right = left + Width;
                int bottom = top + Height;

                shapeRect.X = left;
                shapeRect.Y = top;
                shapeRect.Width = Width;
                shapeRect.Height = Height;

                Path.Reset();
                Path.StartFigure();
                Path.AddRectangle(shapeRect);
                PointF[] a = { new PointF(right, top + Height / 2f - connectorWidth), new PointF((float)right - connectorWidth, top + Height / 2f), new PointF(right, top + Height / 2f + connectorWidth) };
                Path.AddPolygon(a);
                Path.CloseFigure();
                return true;
            }
            return false;
        }


        #region Fields
        private int connectorWidth = 5;
        private Rectangle shapeRect;
        #endregion

        
        #region ISymbol Members

        public BaseComponent initializeComponent(IPresentation p)
        {
            return new EntryPoint(p);
        }

        public virtual int ActionsCount
        {
            get { return 0; }
        }

        public virtual int EventsCount
        {
            get { return 1; }
        }

        public virtual int SettersCount
        {
            get { return 0; }
        }

        public virtual int GettersCount
        {
            get { return 0; }
        }

        public virtual string GetTitleForAction(int id)
        {
            return null;
        }

        public virtual string GetTitleForEvent(int id)
        {
            switch (id)
            {
                case 0:
                    return "Start of the presentation or subdiagram";
            }
            return null;
        }

        public virtual string GetTitleForSetter(int id)
        {
            return null;
        }

        public virtual string GetTitleForGetter(int id)
        {
            return null;
        }

        #endregion

        internal static Shape CreateInstance(ShapeType shapeType, Template template)
        {
            return new EntryPointSymbol(shapeType, template);
        }

    }


}
